package com.rpc.client;

import com.rpc.transport.RequestData;
import com.rpc.transport.ResponseData;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * @author yanzhuoleo
 * @version V1.0
 * @Description: 远程future
 * @date 2018-01-16-下午2:54
 */

public class RpcFuture implements Future<ResponseData> {

    private              CountDownLatch countDownLatch = new CountDownLatch(1);
    private static final Logger         logger         = LoggerFactory.getLogger(RpcFuture.class);
    @Getter
    @Setter
    private RequestData requestData;

    public void setResponseData(ResponseData responseData) {
        countDownLatch.countDown();
        this.responseData = responseData;
    }

    @Getter
    private ResponseData responseData;

    /**
     *
     * @param mayInterruptIfRunning
     * @return
     */
    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        countDownLatch.countDown();
        return false;
    }

    /**
     * TODO
     * @return
     */
    @Override
    public boolean isCancelled() {
        return false;
    }

    /**
     * TODO
     * @return
     */
    @Override
    public boolean isDone() {
        return responseData != null;
    }

    @Override
    public ResponseData get() throws InterruptedException {
        countDownLatch.await();
        return responseData;
    }

    @Override
    public ResponseData get(long timeout,
                            TimeUnit unit) throws InterruptedException {

        countDownLatch.await(timeout, unit);
        return responseData;
    }
}
